package pac;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import BackEnd.IFolio;

public class newStock extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	public newStock(final IFolio folio, final DefaultTableModel model) {
		setTitle("Add A New Stock");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 246, 184);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblStockTicker = new JLabel("Stock Ticker:");
		lblStockTicker.setBounds(10, 24, 87, 14);
		contentPane.add(lblStockTicker);

		JLabel lblNumberOfStocks = new JLabel("Number of Stocks:");
		lblNumberOfStocks.setBounds(10, 51, 128, 14);
		contentPane.add(lblNumberOfStocks);

		JLabel lblCostPerStock = new JLabel("Cost Per Stock:");
		lblCostPerStock.setBounds(10, 76, 103, 14);
		contentPane.add(lblCostPerStock);

		textField = new JTextField();
		textField.setBounds(134, 21, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(134, 48, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(134, 73, 86, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String name = null;
				int num = 0;
				double cost = 0;
				boolean foundTicker = true;

				try {
					name = textField.getText();
					num = Integer.parseInt(textField_1.getText());
					if (textField_2.getText().equals("")) cost = StrathQuoteServer.getLastValue(name);
					else  cost = Double.parseDouble(textField_2.getText());

				} catch (WebsiteDataException e1) {
					foundTicker = false;
					System.out.println(name + " doesnt exist.");
				} catch (NoSuchTickerException e1) {
					foundTicker = false;
					System.out.println(name + " doesnt exist.");
				}

				if (foundTicker == true) {
					folio.buyShares(name, num, cost);

					try {
						double currentValue = StrathQuoteServer
								.getLastValue(name);

						model.addRow(new Object[] { name, num, cost,
								currentValue, num * cost,
								(currentValue * 5 - cost * 5) });
					} catch (Exception e1) {
					}

				}

				close();
			}
		});
		btnAdd.setBounds(24, 112, 89, 23);
		contentPane.add(btnAdd);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(131, 112, 89, 23);
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Dispose of frame
				close();
			}
		});
		contentPane.add(btnCancel);
	}
	
	public void addFolio(IFolio folio, final DefaultTableModel model, String name, int num){
		
		boolean foundTicker = true;
		double cost = 0;
		try {
			
			cost = StrathQuoteServer.getLastValue(name);

		} catch (WebsiteDataException e1) {
			foundTicker = false;
			System.out.println(name + " doesnt exist.");
		} catch (NoSuchTickerException e1) {
			foundTicker = false;
			System.out.println(name + " doesnt exist.");
		}
		if (foundTicker == true) {
			folio.buyShares(name, num, cost);

			try {
				double currentValue = StrathQuoteServer
						.getLastValue(name);

				model.addRow(new Object[] { name, num, cost,
						currentValue, num * cost,
						(currentValue * 5 - cost * 5) });
			} catch (Exception e1) {
			}

		}
	}

	public void close() {
		this.dispose();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}

}
